package com.tianji.learning.mq;

import cn.hutool.core.util.ObjectUtil;
import com.tianji.api.dto.trade.OrderBasicDTO;
import com.tianji.learning.service.ILearningLessonService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;


import static com.tianji.common.constants.MqConstants.Exchange.ORDER_EXCHANGE;
import static com.tianji.common.constants.MqConstants.Key.ORDER_PAY_KEY;

@Slf4j
@Component
@RequiredArgsConstructor
public class LessonChangeListener {

    private final ILearningLessonService lessonService;
    /**
     * 监听订单支付消息->新增课程到用户的课表*/
    @RabbitListener(bindings =@QueueBinding(
            value = @Queue( value = "learning.lesson.pay.queue",durable = "true"),
            exchange = @Exchange(value = ORDER_EXCHANGE,type = ExchangeTypes.TOPIC),
            key = ORDER_PAY_KEY
    ))
    public void listenLessonPay(OrderBasicDTO order){
        if (ObjectUtil.isEmpty(order)||ObjectUtil.isEmpty(order.getCourseIds())||ObjectUtil.isEmpty(order.getUserId())) {
            log.error("订单支付消息格式异常{}:",order);
        }

        log.info("监听到用户：{}的订单：支付消息，需要添加课程{}，至用户课表{}",order.getUserId(),order.getCourseIds(),order.getUserId());
        lessonService.addLessons(order.getUserId(),order.getCourseIds());

    }
}
